package com.freesun.shop.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.demo.trade.config.Configs;
import com.alipay.demo.trade.service.AlipayTradeService;
import com.alipay.demo.trade.service.impl.AlipayTradeServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.FileWriter;
import java.io.IOException;

/**
 * 配置类
 */
@Configuration
public class AliPayConfig {
//↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public static String app_id = "2016101300679312";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCXALlV8fQTBkzR17oi//IO02iTiqbtURG1SvE2U3fRjdpNNPdI/tOXUxgT1jY2zz31iwi/c1LKDpw3dr2C4ZWsXh9YB+dovtYYLEnp5mai+4pCjm6Wc8BwWBlfyH1b/tMmdojKv3G/Bk29oaDfkUp9plvp+IDuE4pZbGEqebLCU0yBd9YRfYwm+ExRSKVz4rh/UNPi+1FaB1zOffcYbP8tKKt/0Bgsui5Els7prm8mte3/fMcXof1QaCKs85hfrjkpe/HO/iSsStHuav0NFVt0G3wxf0/Z67TAfLZwXdnmPQfSxjxDta4oQdaa9JkSztw8nyL/c1dFBhxi9niExuxJAgMBAAECggEAYZujMc4LwnxxDbfP5y3gzvqS/8RBcOfVdYXy8/XyXq++K+gP7MxkA84/oxr1wmomF+F+jxrBqkDVc/i6Oa8OK/RPeHzrdcbd55RBVwOK1tGYJrug+nnqK/SWulaDygKnhJZciApGQX+WovymW720FBKEGXJ8DOjupi0rheRSy89BQw3Gi7EJEpCuWiNVzNbgRSawk497Xf9QJkyK3Ndr4ButFEK60iPM3BWnN6ZlYZkCFQraKCd44LJ1KX80MDxbG4RXiSNcxfseFs5Llc1apD30XG7yKUstflcoHXqwx+lhLpwNnNEj4mffsDbMZ/zh7UZhyoaXvGfcvjkDtaf1nQKBgQDlrSiOXX3KzXoxpSXMpUM4b/y+XLPw1PXFj+NC4Qx50nHx0cKtxsvxx//IkbUX4Z6m6vjq2qChXMEIy/ivrR1LVVmBNpsa3vWok+ZMGWA6zpdjWdHXpE19sIFn9MGDBtEi/Vk2MIbZHiA+0CARIqSBoqdJkSX7OI/irxoAkvZbTwKBgQCoTzyBwvO0+dVPtPqbXFoJAaKe5IgB6FVHFJChGZbeDnWyAv59KA1vs65TJgG11LgEgQYUdNU2/eAfr7o1WCpb5Aftx4LIYmoTcrlrjzygks2heodge69Gmhc490Uc7DUeljh6eeW/9VCELqVLi6ysAwxVsZ52EeQP7stJ5hr45wKBgQCsBDBnz+WyyopGPGY/C1d5IYauHEg4i4c3XDjdkigB5XepoWypMkb04tHTqi1zoWttoHaljvDXady5derfyEw0a8wJVph8W5iplXvaavcDlE8V/eOIGmpfRJ7m+Q0l0Sk5h34Vc+rLGiEUalRaARQsc2HPW4KS0R4SrQpQe8E/SQKBgFCI8Zvpq/F55h7oq02xyActOCItmgLHqB6F8ZqUgwMoChsuoZVDmAJOCOkSc5kM+yx0jHM3YFqa6iZfUIt773De0QhvAd5ecQFzUNPtywXOmQRee+6n4Du5OclxUCnBwL0tBkNclpneiTNmmoNmhy4bm0hZ4EgA0Y7qJW3r5BYbAoGBAK2mtOK91xbIfXgbiES3J5FWfgoljDoHnMBmPICEeqCrcpjxmFQy5gWHgRln3tlS/wRB410GOQuQm5P/ap3ErNIoCPzEpUhyTE1lkRfPdGfZmSVKx2sPLCfa9AbD8jqa0K9fNpXYaqBblpM0RDjtvaeWZarzf6mzOyWETO+2kn0k";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkHwQoLjm69VuxizWmuh7uJWWgexKNtGa8iBp6dsR3IzkG5113yD18dD7KZAJ8juRdLBpIeeNwdJd8CdT9LdLrbo9ZWcheI/cEeMCE/2oSZ9vXxaTo+Dd0xkcfG0+GvU3+/7EQqHVEEgRiymLVCCI9flbW0AXayncbv0vFG7jc9cHdYt5vYBlVekyoA8ocxa+9Oetb9D4IX354tHA+sHMGBTn7nBIIDiSZNLOq20je3pUEjMpAtnHX3Nb3UWwFtaVvD57UsM9xXTjwGRFCDhVoSSYWqHskGGmpOcNBEjJLlB8ENa2u9WmcYwWijqWYCmokwK/NrpN3nbNSr5Xy0f66wIDAQAB";

    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "http://localhost:8080/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String return_url = "http://localhost:8080/alipay.trade.page.pay-JAVA-UTF-8/return_url.jsp";

    // 签名方式
    public static String sign_type = "RSA2";

    // 字符编码格式
    public static String charset = "utf-8";

    // 支付宝网关
    public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    // 支付宝网关
    public static String log_path = "C:\\";


//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

    /**
     * 写日志，方便测试（看网站需求，也可以改成把记录存入数据库）
     * @param sWord 要写入日志里的文本内容
     */
    public static void logResult(String sWord) {
        FileWriter writer = null;
        try {
            writer = new FileWriter(log_path + "alipay_log_" + System.currentTimeMillis()+".txt");
            writer.write(sWord);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Bean
    public AlipayTradeService alipayTradeService() {

        /** 一定要在创建AlipayTradeService之前调用Configs.init()设置默认参数
         *  Configs会读取classpath下的zfbinfo.properties文件配置信息，如果找不到该文件则确认该文件是否在classpath目录
         */
        Configs.init("zfbinfo.properties");

        /** 使用Configs提供的默认参数
         *  AlipayTradeService可以使用单例或者为静态成员对象，不需要反复new
         */
        return new AlipayTradeServiceImpl.ClientBuilder().build();
    }
    @Bean
    public AlipayClient alipayClient(){
        AlipayClient alipayClient = new DefaultAlipayClient(
                AliPayConfig.gatewayUrl, AliPayConfig.app_id, AliPayConfig.merchant_private_key,
                "json", AliPayConfig.charset, AliPayConfig.alipay_public_key, AliPayConfig.sign_type);

        return alipayClient;
    }

}
